<?php

require_once dirname(__FILE__) . '/../Board/SpaceMaker.php';
require_once dirname(__FILE__) . '/../Board/GroupMaker.php';

class Monopoly_Board
{
    private $_spaces = null;
    private $_groups = array();

    public function __construct($space_maker, $group_maker)
    {
        $properties = array();
        $go = $space_maker->make('Go', $this);

        // brown group
        $group = $group_maker->make('brown');
        $properties['mediterranean'] = $space_maker->make('Mediterranean Avenue', $this)
                                                   ->setGroup($group);
        $properties['baltic'] = $space_maker->make('Baltic Avenue', $this)
                                            ->setGroup($group);
        // cyan group
        $group = $group_maker->make('cyan');
        $properties['oriental'] = $space_maker->make('Oriental Avenue', $this)
                                              ->setGroup($group);
        $properties['vermont'] = $space_maker->make('Vermont Avenue', $this)
                                             ->setGroup($group);
        $properties['connecticut'] = $space_maker->make('Connecticut', $this)
                                                 ->setGroup($group);
        // purple group
        $group = $group_maker->make('purple');
        $properties['st_charles'] = $space_maker->make('St. Charles Place', $this)
                                                ->setGroup($group);
        $properties['states'] = $space_maker->make('States Avenue', $this)
                                            ->setGroup($group);
        $properties['virginia'] = $space_maker->make('Virginia Avenue', $this)
                                             ->setGroup($group);
        // orange group
        $group = $group_maker->make('orange');
        $properties['st_james'] = $space_maker->make('St. James Place', $this)
                                              ->setGroup($group);
        $properties['tennessee'] = $space_maker->make('Tennessee Avenue', $this)
                                               ->setGroup($group);
        $properties['new_york'] = $space_maker->make('New York Avenue', $this)
                                             ->setGroup($group);
        // red group
        $group = $group_maker->make('red');
        $properties['kentucky'] = $space_maker->make('Kentucky Avenue', $this)
                                              ->setGroup($group);
        $properties['indiana'] = $space_maker->make('Indiana Avenue', $this)
                                             ->setGroup($group);
        $properties['illinois'] = $space_maker->make('Illinois Avenue', $this)
                                              ->setGroup($group);
        // yellow group
        $group = $group_maker->make('yellow');
        $properties['atlantic'] = $space_maker->make('Atlantic Avenue', $this)
                                              ->setGroup($group);
        $properties['ventnor'] = $space_maker->make('Ventnor Avenue', $this)
                                             ->setGroup($group);
        $properties['marvin'] =  $space_maker->make('Marvin Gardens', $this)
                                             ->setGroup($group);
        // green group
        $group = $group_maker->make('green');
        $properties['pacific'] = $space_maker->make('Pacific Avenue', $this)
                                             ->setGroup($group);
        $properties['nc_ave'] = $space_maker->make('North Carolina Avenue', $this)
                                            ->setGroup($group);
        $properties['pennsylvania'] = $space_maker->make('Pennsylvania Avenue', $this)
                                                  ->setGroup($group);
        // blue group
        $group = $group_maker->make('blue');
        $properties['park'] = $space_maker->make('Park Place', $this)
                                             ->setGroup($group);
        $properties['board'] = $space_maker->make('Boardwalk', $this)
                                            ->setGroup($group);
       
        // build board
        $go->next($properties['mediterranean'])
           ->next($space_maker->make('Community Chest', $this))
           ->next($properties['baltic'])
           ->next($space_maker->make('Income Tax', $this))
           ->next($space_maker->make('Reading Railroad', $this))
           ->next($properties['oriental'])
           ->next($space_maker->make('Chance', $this))
           ->next($properties['vermont'])
           ->next($properties['connecticut'])
           ->next($space_maker->make('Just Visiting', $this))
           ->next($properties['st_charles'])
           ->next($space_maker->make('Electric Company', $this))
           ->next($properties['states'])
           ->next($properties['virginia'])
           ->next($space_maker->make('Pennsylvania Railroad', $this))
           ->next($properties['st_james'])
           ->next($space_maker->make('Community Chest', $this))
           ->next($properties['tennessee'])
           ->next($properties['new_york'])
           ->next($space_maker->make('Free Parking', $this))
           ->next($properties['kentucky'])
           ->next($space_maker->make('Chance', $this))
           ->next($properties['indiana'])
           ->next($properties['illinois'])
           ->next($space_maker->make('B&O Railroad', $this))
           ->next($properties['atlantic'])
           ->next($properties['ventnor'])
           ->next($space_maker->make('Water Works', $this))
           ->next($properties['marvin'])
           ->next($space_maker->make('Go To Jail', $this))
           ->next($properties['pacific'])
           ->next($properties['nc_ave'])
           ->next($space_maker->make('Community Chest', $this))
           ->next($properties['pennsylvania'])
           ->next($space_maker->make('Short Line', $this))
           ->next($space_maker->make('Chance', $this))
           ->next($properties['park'])
           ->next($space_maker->make('Luxury Tax', $this))
           ->next($properties['board'])
           ->next($go);

        $this->setSpaces($go);
    }

    public function setSpaces($spaces)
    {
        $this->_spaces = $spaces;
    }

    public function getSpaces()
    {
        return $this->_spaces;
    }

    public function getSpace($name)
    {
        $spaces = $this->getSpaces();

        $start_name = $spaces->getName();

        while (true) {
            if ($name == $spaces->getName()) {
                return $spaces;
            }

            $spaces = $spaces->next();

            if ($spaces->getName() == $start_name) {
                return false;
            }
        } 
    }
}
